package com.appkubes.mall.restful.application.cart.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.core.entity.MallCart;
import com.appkubes.mall.restful.application.cart.entity.model.CartResult;
import com.appkubes.mall.restful.application.cart.entity.vo.*;
import com.appkubes.mall.restful.application.cart.service.CartService;
import com.appkubes.mall.restful.application.common.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api("购物车模块")
@RequestMapping("/cart")
public class CartController extends BaseController {

    @Autowired
    private CartService cartService;

    @ApiOperation("加入购物车")
    @PostMapping("/joinCart")
    public Object joinCart(@RequestBody @ParamValid CartAddVO cartAddVO) {
        MallCart cart = cartService.getCartByIds(cartAddVO.getUserKey(),
                cartAddVO.getGoodsKey(), cartAddVO.getStoreKey(), cartAddVO.getAttrKeys());
        if (cart == null) {
            cart = new MallCart();
        }
        Integer nums = null != cart.getNum() ? cart.getNum() + cartAddVO.getNum() : cartAddVO.getNum();

        MallCart newCart = new MallCart();
        newCart.setUserKey(cartAddVO.getUserKey());
        newCart.setGoodsKey(cartAddVO.getGoodsKey());
        newCart.setStoreKey(cartAddVO.getStoreKey());
        newCart.setAttrKeys(cartAddVO.getAttrKeys());
        newCart.setAttrNames(cartAddVO.getAttrNames());
        newCart.setActivityKey(cartAddVO.getActivityKey());
        newCart.setNum(nums);
        cart.setCreateTime(DateUtils.DateToTimestamp(new Date()));
        cart.setUpdateTime(cart.getCreateTime());
        Boolean flag = cartService.save(newCart);

        if (flag) {
            CartResult result = new CartResult();
            result.setCartKey(cart.getCartKey());
            Map<String, Object> map = new HashMap<>();
            map.put("cart_key", result.getCartKey());
            return Response.success(map);
        }

        return Response.error("加入购物车失败");
    }

    @ApiOperation("删除购物车商品")
    @PostMapping("/deleteCarts")
    public Object deleteCarts(@RequestBody @ParamValid  CartDeleteVO cartDeleteVO) {
        Boolean flag = cartService.removeById(cartDeleteVO.getCartKeys());
        if (flag) {
            return Response.success("删除成功");
        } else {
            return Response.error("删除失败");
        }
    }

    @ApiOperation("获取购物车商品数量")
    @PostMapping("/getCartNum")
    public Object getCartNum(@RequestBody @ParamValid CartNumVO cartNumVO) {
        CartResult result = new CartResult();
        result.setCartNum(cartService.getCartNums(cartNumVO.getUserKey()));
        return Response.success(result.getCartNum());
    }

    @ApiOperation("获取购物车商品")
    @PostMapping("/getCarts")
    public Response getCarts(@RequestBody @ParamValid CartGetVO cartGetVO) {
        return imgResponse(cartService.getCarts(cartGetVO));
    }

    @ApiOperation("更新购物车")
    @PostMapping("/updateCart")
    public Object updateCart(@RequestBody @ParamValid CartUpdateVO cartUpdateVO) {
        String msg = "";
        Boolean result = false;
        if (null == cartUpdateVO.getNum() || cartUpdateVO.getNum() <= 0) {
            result = cartService.removeById(cartUpdateVO.getCartKey());
            msg = "删除";
        } else {
            msg = "修改";
            MallCart cart = cartService.getById(cartUpdateVO.getCartKey());
            if (null != cart) {
                if (cart.getNum()!=cartUpdateVO.getNum()) {
                    cart.setNum(cartUpdateVO.getNum());
                    result = cartService.updateById(cart);
                }
            }
        }

        if (result) {
            return Response.success(msg.concat("成功"));
        } else {
            return Response.error(msg.concat("失败"));
        }
    }
}
